You’re given two dataframes: transactions
and products
.
The transactions dataframe contains transaction ids, product ids, and the total amount of each product sold.
The products dataframe contains product ids and prices.
Write a function to return a dataframe containing every transaction with a total value of over $100. Include the total value of the transaction as a new column in the dataframe.
transactions
和 products
依相同欄位進行合併 【Day 17|資料合併的三種常用語法】import pandas as pd
def transactions_over_100(df_transactions: pd.DataFrame, df_products: pd.DataFrame):
# 關聯合併 merge()
new = pd.merge(df_transactions,df_products,on='product_id')
# total value over 100
new['total_value'] = new['amount'] * new['price']
# 資料篩選
con = new['total_value'] > 100
# 刪除price欄位
new.drop(columns='price',inplace=True)
return new.loc[con]
Pandas 中的 drop**( index = '索引值名稱',** columns = '欄位名稱', inplace = True 或 False)
操作目的在於「刪除行或列的資料」,與 dropna( )
刪除缺失值使用時機不同。
嗨,我是 Eva,一位正在努力跨進資料科學領域的女子!
如果有任何不理解、錯誤或其他方法想分享的話,歡迎留言!
喜歡的話,也歡迎按讚訂閱唷!我們明天見!